﻿
Namespace LightSwitchApplication

    Public Class PayHistoryItem



        Private Sub NetPay_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = TaxableGross - If(WTax Is Nothing, 0, WTax) - SumAdditionalDeductionsAfterTax + SumAdditionalNonTaxableIncome
        End Sub

        Private Sub GrossPay_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = RegWage + RegOTTotal + SunOTTotal + HolOTTotal + SumAdditionalTaxableIncome + If(Incentives Is Nothing, 0, Incentives) + If(Allowance Is Nothing, 0, Allowance) + Leaves
        End Sub

        Private Sub RegWage_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = (Rate / LightPayConstants.workHoursPerDay) * If(NoOfHoursWorked Is Nothing, 0, NoOfHoursWorked)
        End Sub

        Private Sub RegOTTotal_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = (Rate / LightPayConstants.workHoursPerDay) * If(RegOT Is Nothing, 0, RegOT)
        End Sub

        Private Sub SunOTTotal_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = (Rate / LightPayConstants.workHoursPerDay) * If(SunOT Is Nothing, 0, SunOT)
        End Sub

        Private Sub HolOTTotal_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = (Rate / LightPayConstants.workHoursPerDay) * If(HolidayOT Is Nothing, 0, HolidayOT)
        End Sub

        'Private Sub TotalAdditionalIncome_Compute(ByRef result As Decimal)
        '    ' Set result to the desired field value
        '    result = If(PayHistoryMoreIncomeItem.Count = 0, 0, PayHistoryMoreIncomeItem.Sum(Function(x) x.Amount))
        'End Sub


        'Private Sub TotalDeduction_Compute(ByRef result As Decimal)
        '    ' Set result to the desired field value
        '    result = If(SSS_MCR Is Nothing, 0, SSS_MCR) + If(WTax Is Nothing, 0, WTax) + TotalDeductionsBeforeTax + (If(Lates Is Nothing, 0, Lates) * (Rate / 8))
        'End Sub

        Private Sub SumAdditionalDeductionsBeforeTax_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            Try
                result = If(PayHistoryMoreDeductions.Count = 0, 0, PayHistoryMoreDeductions.Where(Function(y) y.RefAdditionalDeductionItem.PreTax = True).Sum(Function(x) x.Amount))
            Catch ex As Exception

            End Try

        End Sub

        Private Sub SumAdditionalDeductionsAfterTax_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            Try
                result = If(PayHistoryMoreDeductions.Count = 0, 0, PayHistoryMoreDeductions.Where(Function(y) y.RefAdditionalDeductionItem.PreTax = False).Sum(Function(x) x.Amount))
            Catch ex As Exception

            End Try
        End Sub

        Private Sub TaxableGross_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = GrossPay - TotalDeductionsBeforeTax
        End Sub


        Private Sub SumAdditionalTaxableIncome_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = If(PayHistoryMoreIncomeItem.Count = 0, 0, PayHistoryMoreIncomeItem.Where(Function(y) y.Taxable = True).Sum(Function(x) x.Amount))
        End Sub

        Private Sub TotalDeductionsBeforeTax_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = SumAdditionalDeductionsBeforeTax + If(SSS_MCR Is Nothing, 0, SSS_MCR)
        End Sub

        Private Sub SumAdditionalNonTaxableIncome_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = If(PayHistoryMoreIncomeItem.Count = 0, 0, PayHistoryMoreIncomeItem.Where(Function(y) y.Taxable = False).Sum(Function(x) x.Amount))
        End Sub

        Private Sub Leaves_Compute(ByRef result As Decimal)
            ' Set result to the desired field value
            result = If(PayHistoryLeavesCollection.Count = 0, 0, PayHistoryLeavesCollection.Sum(Function(x) x.Amount))
            'Dim filedLeaves = From fLeaves In Me.DataWorkspace.ApplicationData.LeaveHistories
            '                  Where fLeaves.EmployeeItem.EmployeeID = EmployeeItem.EmployeeID AndAlso
            '                  ((CDate(fLeaves.EndDate) >= CDate(PayPeriodItem.StartDate) AndAlso CDate(fLeaves.EndDate) <= CDate(PayPeriodItem.EndDate)) OrElse
            '                   (CDate(fLeaves.StartDate) >= CDate(PayPeriodItem.StartDate) AndAlso CDate(fLeaves.StartDate) <= CDate(PayPeriodItem.EndDate)))

            'For Each item As LeaveHistory In filedLeaves
            '    Dim computeStart As Date = Nothing
            '    Dim computeEnd As Date = Nothing
            '    Dim leaveDays As Integer = 0

            '    If item.Unit.ToUpper = "DAYS" Then
            '        If item.StartDate < PayPeriodItem.StartDate Then
            '            computeStart = PayPeriodItem.StartDate
            '        Else
            '            computeStart = item.StartDate
            '        End If


            '        If item.EndDate < PayPeriodItem.EndDate Then
            '            computeEnd = item.EndDate
            '        Else
            '            computeEnd = PayPeriodItem.EndDate
            '        End If

            '        While computeStart <= computeEnd
            '            If Not (computeStart.DayOfWeek = DayOfWeek.Saturday Or computeStart.DayOfWeek = DayOfWeek.Sunday) Then
            '                leaveDays += 1
            '            End If
            '            computeStart += TimeSpan.FromDays(1)
            '        End While
            '        'Leave rate less Saturday and Sunday
            '        result += leaveDays * Rate
            '    Else
            '        result += item.Amount * (Rate / LightPayConstants.workHoursPerDay)
            '    End If

            'Next
        End Sub
    End Class

End Namespace
